//获取应用实例
const app = getApp()
let utils = require('../../../utils/util.js')
// pages/sms/sms.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //发送验证码按钮是否是禁用样式
    verCodeBtnDisabledClass: false,
    //按钮是否禁用
    verCodeBtnDisabled: false,
    //电话号码
    mobile: '',
    //验证码的文字
    verCodeBtn: '发送',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  //获取用户手机号码
  getPhoneNumber: function(e) {
    let self = this;
		console.log(e.detail);
    if (app.userInfo.ThirdUserID) {
      self.addUser(e.detail.iv, e.detail.encryptedData)
    } else {
      app.addUserAuth(function() {
        self.addUser(e.detail.iv, e.detail.encryptedData)
      });
    }
  },
  /**
   * 添加用户
   */
  addUser(iv, encryptedData) {
    let self = this;
    let url = app.apiBaseUrl + '/WxMiniAppointment/AddUser';
    app.httpRequest({
      url: url,
      showLoading: false,
      method: "POST",
      data: {
        Sessionkey: app.userInfo.Sessionkey,
        ThirdUserSource: 3,
        ThirdUserID: app.userInfo.ThirdUserID,
        iv: iv,
        encryptedData: encryptedData
      },
      success: function(res) {
        let rdata = res.data;
        console.log(res);
        if (rdata.status == 0) {
          app.userInfo.UserID = rdata.data.UserID;
          app.userInfo.AuthLevel = 0;
          app.userInfo.UserMobile = rdata.data.UserMobile;
          app.setUserInfo(app.userInfo);
          wx.navigateTo({
            url: '/pages/liveAuthentication/ocr/ocr',
          });
        } else {
          wx.showToast({
            title: '手机号绑定失败，请重新验证',
            icon: 'none'
          });
        }
      }
    });
  },
  //表单提交
  formSubmit(e) {
    let formData = e.detail.value;
    if (utils.isNullOrEmpty(formData.Mobile)) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      });
      return false;
    };

    if (!utils.isMoile(formData.Mobile)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      });
      return false;
    };

    if (utils.isNullOrEmpty(formData.verCode)) {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none'
      });
      return false;
    };

    let url = app.apiBaseUrl + '/AuthenticationAI/AddUserMobile';
    app.httpRequest({
      url: url,
      showLoading: true,
      method: "POST",
      data: {
        UserMobile: formData.Mobile,
        VerCode: formData.verCode,
        ThirdUserSource: 3,
        ThirdUserID: app.userInfo.ThirdUserID
      },
      success: function(res) {
        let rdata = res.data;
        if (rdata.status == 0) {
          //设置用户信息
          app.userInfo.UserID = rdata.data;
          app.userInfo.AuthLevel = 0;
          app.userInfo.UserMobile = formData.UserMobile;
          app.setUserInfo(app.userInfo);

          //跳转身份证上传
          wx.navigateTo({
            url: '/pages/liveAuthentication/ocr/ocr',
          });
        } else {
          wx.showToast({
            title: '手机号绑定失败，请重新验证',
            icon: 'none'
          });
        }
      }
    });
  },
  /*
   * 获取验证码
   */
  verCodeTap() {
    let self = this;
    let idx = 59;
    if (utils.isNullOrEmpty(self.data.mobile)) {
      wx.showToast({
        title: '请输入电话号码',
        icon: 'none'
      });
      return false;
    };


    if (!utils.isMoile(self.data.mobile)) {
      wx.showToast({
        title: '请输入正确的电话号码',
        icon: 'none'
      });
      return false;
    };

    self.setData({
      verCodeBtn: idx + '秒',
      verCodeBtnDisabled: true,
      verCodeBtnDisabledClass: true
    });

    let t = setInterval(function() {
      idx--;
      self.setData({
        verCodeBtn: idx + '秒'
      });
      if (idx <= 0) {
        clearInterval(t);
        self.setData({
          verCodeBtn: '验证码',
          verCodeBtnDisabled: false,
          verCodeBtnDisabledClass: false
        })
      }
    }, 1000)

    let url = app.globalData.apiMsgUrl + "/SMS/SendCheckCode";

    app.httpRequest({
      method: "POST",
      url: url,
      showLoading: false,
      data: {
        Mobile: self.data.mobile,
        PlatformId: 100,
        ChannelId: 2,
        SendTypeName: "车管通用户认证验证码"
      },
      success: function(res) {
        let rdata = res.data;
        if (rdata.status == 0) {

        } else {
          wx.showToast({
            title: rdata.message,
            icon: "none"
          });
        }
      }
    });
  },
  /**
   * 电话号码失去焦点
   */
  mobileInput(e) {
    this.setData({
      mobile: e.detail.value
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})